package cn.jiangjiesheng.bootstrap.cloud.service.validator;

import cn.hutool.core.collection.CollUtil;
import com.google.common.base.Joiner;
import cn.jiangjiesheng.bootstrap.cloud.service.ProjectMetadataProperties;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import java.util.List;

/**
 * 描述：配置文件校验
 *
 * @auth: panyu
 * @createDate: 2023/5/19  14:20
 */
@Component("configurationPropertiesValidator")
@Slf4j
public class ConfigPropertiesValidator implements Validator {

    @Override
    public boolean supports(@NotNull Class<?> clazz) {
        return ProjectMetadataProperties.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(@NotNull Object target, @NotNull Errors errors) {

        if(target instanceof ProjectMetadataProperties){
            ProjectMetadataProperties config = (ProjectMetadataProperties) target;
            List<String> errorList = config.checkEntId();
            if(CollUtil.isNotEmpty(errorList)){
                errors.rejectValue("", "", "[project-metadata.entIdList]的以下容器ID配置错误："+ Joiner.on(",").join(errorList));
            }
        }

    }
}
